﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Content;

enum switchState
{
    off,
    off_selected,
    on,
    on_selected
}

namespace Surveillance
{
    public class Switch 
    {
        #region switch declarations

        public static Texture2D switchTexture;
        public static SpriteFont font;
        public static readonly Dictionary<string, int> SWITCH_STATES = new Dictionary<string, int> 
        { 
            { "off", 0 }, 
            { "off_selected", 20 }, 
            { "on", 40 }, 
            { "on_selected", 60 }
        };
        private Rectangle frame;
        private Vector2 worldPosition;
        private Rectangle targetRectangle;
        private switchState state;
        private MouseState oldMouseState;
        private List<Switchable> linkedTo;
        private int biasX;
        private int biasY;
        

        #endregion

        #region switch constructors

        public Switch(Vector2 position, int width, int height, int bX, int bY)
        {
            worldPosition = position;
            targetRectangle = new Rectangle((int)position.X, (int)position.Y, width, height);
            frame = new Rectangle(SWITCH_STATES["off"],0,20,20);
            state = switchState.off;
            oldMouseState = Mouse.GetState();
            linkedTo = new List<Switchable>();
            biasX = bX;
            biasY = bY;
        }

        #endregion

        #region switch update and draw


        private bool click(MouseState mouseState)
        {
            return (mouseState.LeftButton == ButtonState.Released && oldMouseState.LeftButton == ButtonState.Pressed);
        }

        public void Draw(SpriteBatch spriteBatch)
        {
            spriteBatch.Draw(switchTexture,targetRectangle,frame,Color.White);
        }

        #endregion

        #region switch auxiliary

        public void mark()
        {
            if (state == switchState.off)
            {
                state = switchState.off_selected;
                frame.X = SWITCH_STATES["off_selected"];
            }
            else if (state == switchState.on)
            {
                state = switchState.on_selected;
                frame.X = SWITCH_STATES["on_selected"];
            }
        }

        public void unmark()
        {
            if (state == switchState.off_selected)
            {
                state = switchState.off;
                frame.X = SWITCH_STATES["off"];
            }
            else if (state == switchState.on_selected)
            {
                state = switchState.on;
                frame.X = SWITCH_STATES["on"];
            }
        }

        public void click()
        {
            if (state == switchState.off_selected || state == switchState.off)
            {
                state = switchState.on_selected;
                frame.X = SWITCH_STATES["on_selected"];
            }
            else if (state == switchState.on_selected || state == switchState.on)
            {
                state = switchState.off_selected;
                frame.X = SWITCH_STATES["off_selected"];
            }
            foreach (Switchable link in linkedTo)
            {
                link.doSwitch(link.Def);
                link.Def = link.Def * -1;
            }
        }

        public void addLink(Switchable link, int def)
        {
            link.Def = def;
            linkedTo.Add(link);
        }

        public void reset()
        {
            state = switchState.off;
            frame.X = SWITCH_STATES["off"];
        }

        #endregion

        #region switch properties

        public Rectangle TargetRectangle
        {
            get { return targetRectangle; }
            set { targetRectangle = value; }
        }

        public int BiasX
        {
            get { return biasX; }
            set { biasX = value; }
        }

        public int BiasY
        {
            get { return biasY; }
            set { biasY = value; }
        }

        #endregion

      
    }
}
